<template>
  <div class="star" :class="starType">
    <span class="star-item" v-for="itemClass in itemClasses" :class="itemClass" ></span>
  </div>
</template>

<script>
  const LENGTH = 5;
  const C_ON = 'icon-star-full';
  const C_HALF = 'icon-star-half';
  const C_OFF = 'icon-star-empty';

  export default {
    props: {
      size: {
        type: Number
      },
      score: {
        type: Number
      }
    },
    name: '',
    data () {
      return {}
    },
    computed: {
      starType () {
        return 'star-' + this.size;
      },
      itemClasses () {
        let res = [];
        let HasDecimal = this.score ? this.score % 1 !== 0 : 0;
        let score = Math.floor(this.score);
        for (let i = 1; i <= score; i++) {
          res.push(C_ON);
        }
        if (HasDecimal) {
          res.push(C_HALF);
        }
        while (res.length < LENGTH) {
          res.push(C_OFF)
        }
        return res;
      }
    }

  }
</script>

<style scoped lang="less">
  @import '../../common/less/variable';
  .star{
    color:@color-theme;
  }
  .star-24{
    .star-item{
      width: 10px;
      height: 10px;
      display: inline-block;
      margin-right: 5px;
      background-size: 10px 10px;
      &:last-child{
        margin-right: 0;
      }
    }
  }

  .star-48{
    .star-item{
      width: 20px;
      height: 20px;
      display: inline-block;
      margin-right: 5px;
      background-size: 20px 20px;
      &:last-child{
        margin-right: 0;
      }
    }
  }
</style>
